<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp" %>
<%@ taglib prefix="his_fns" uri="/WEB-INF/tlds/his_fns.tld" %>

<div class="app">
    <section class="layout">
        <section class="main-content col-lg-12" id="stcokLeftDiv">
            <div class="content-wrap">
                <div class="wrapper" style="bottom: 50px;">
                    <ul class="nav nav-tabs">
                        <li class="active">
                            <a href="#" onclick="loadDiv('mainCenterDiv','${ctx}/csm/expStock/impExpIndex')">对消入出库</a>
                        </li>
                    </ul>
                    <section class="panel panel-default">
                        <div class="panel-body">
                            <div class="table-responsive no-border">
                                <div class="datatable-top">
                                    <div class="pull-left">
                                        <form:form id="searchForm" modelAttribute="expStock"
                                                   onsubmit="return loadDivForm('mainCenterDiv','searchForm','${ctx}/csm/expStock/impExpIndex');"
                                                   method="post" class="form-inline">
                                            <div class="form-group">
                                                <label class=" control-label">入库类别：</label>
                                                <form:select path="expImportMaster.importClass" id="importClass" htmlEscape="false" class="form-control" maxlength="1"
                                                             onchange="loadSupplier()" data-parsley-required="true">
                                                    <form:options items="${his_fns:getImpOrOutpList('1')}" itemLabel="inOutName" itemValue="type" htmlEscape="false" />
                                                </form:select>
                                            </div>
                                            <div class="form-group">
                                                <label class=" control-label">供货单位：</label>
                                                <form:select path="expImportMaster.supplier" id="supplier" htmlEscape="false" class="form-control"  data-parsley-required="true">
                                                    <%--<form:options items="${his_fns:getImpOrOutpList('1')}" itemLabel="inOutName" itemValue="type" htmlEscape="false" />--%>
                                                </form:select>
                                            </div>
                                            <div class="form-group">
                                                <label class=" control-label">日期：</label>
                                                <input name="startDate" id="startDate" htmlEscape="false" class="form-control Wdate" data-parsley-required="true" readonly
                                                       onfocus="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss'})" value='<fmt:formatDate value="${sysdate}" pattern="yyyy-MM-dd HH:mm:ss"/>' />
                                            </div>
                                            <div class="form-group">
                                                <label class=" control-label">付款方式：</label>
                                                <form:select path="expImportMaster.accountType" id="accountType" htmlEscape="false" class="form-control" maxlength="1" data-parsley-required="true">
                                                    <form:option value="" label=""></form:option>
                                                    <form:options items="${fns:getDictList('ACCOUNT_TYPE')}" itemLabel="label" itemValue="value" htmlEscape="false" />
                                                </form:select>
                                            </div>
                                            <div class="form-group">
                                                    <input id="btnSubmit" class="btn btn-primary" type="button" onclick="saveImpExp()" value="保存"/>
                                                    <input id="addExp" class="btn btn-primary" type="button" value="增加" onclick="addExpList()"/>
                                            </div><br/><br />

                                            <div class="form-group">
                                                <label class=" control-label">出库类别：</label>
                                                <form:select path="expExportMaster.exportClass" id="exportClass" htmlEscape="false" class="form-control" maxlength="1"
                                                             onchange="loadSupplier()" data-parsley-required="true">
                                                    <form:options items="${his_fns:getImpOrOutpList('0')}" itemLabel="inOutName" itemValue="type" htmlEscape="false" />
                                                </form:select>
                                            </div>
                                            <div class="form-group">
                                                <label class=" control-label">收货科室：</label>
                                                <form:select path="expExportMaster.receiver" id="receiver" htmlEscape="false" class="form-control" maxlength="1"
                                                             data-parsley-required="true">
                                                    <%--<form:options items="${his_fns:getImpOrOutpList('1')}" itemLabel="inOutName" itemValue="type" htmlEscape="false" />--%>
                                                </form:select>
                                            </div>
                                            <div class="form-group">
                                                <label class=" control-label">合计：</label>
                                                <form:input path="" id="totalMoney" htmlEscape="false" class="form-control" maxlength="64" />
                                            </div>
                                        </form:form>
                                    </div>
                                </div>
                                <table id="contentTable" class="table table-bordered table-striped mg-t datatable">
                                    <thead>
                                    <tr>
                                        <th hidden>jsonText</th>
                                        <th hidden>infoId</th>
                                        <th hidden>代码</th>
                                        <th>名称</th>
                                        <th hidden>包装数量</th>
                                        <th>规格</th>
                                        <th hidden>包装单位代码</th>
                                        <th>单位</th>
                                        <th hidden>最小规格</th>
                                        <th hidden>最小单位代码</th>
                                        <th hidden>最小单位</th>
                                        <th hidden>厂家ID</th>
                                        <th>厂家</th>
                                        <th>进价</th>
                                        <th>批号</th>
                                        <th>有效期</th>
                                        <th hidden>灭菌方式代码</th>
                                        <th>灭菌方式</th>
                                        <th>灭菌批号</th>
                                        <th>灭菌日期</th>
                                        <th>数量</th>
                                        <th>操作</th>
                                    </tr>
                                    </thead>
                                    <tbody>
                                </table>
                            </div>
                        </div>
                    </section>
                </div>
            </div>
        </section>

        <%--右侧消耗品规格字典列表--%>
        <section class="main-content col-lg-4" style="display: none" id="stcokRightDiv">
            <div class="content-wrap" id="rigthCenterDiv">
            </div>
        </section>
    </section>
</div>
<script type="text/javascript">
    //设置出库日期为当前时间,不能编辑
    var date = new Date();
    var nowMonth = "";
    if (date.getMonth() + 1 <= 9) {
        nowMonth = "0" + (date.getMonth() + 1) + "";
    } else {
        nowMonth = (date.getMonth() + 1) + "";
    }
    var hours = "";
    if (date.getHours() < 10) {
        hours = "0" + date.getHours();
    } else {
        hours = date.getHours();
    }
    var minutes = "";
    if (date.getMinutes() < 10) {
        minutes = "0" + date.getMinutes();
    } else {
        minutes = date.getMinutes();
    }
    var seconds = "";
    if (date.getSeconds() < 10) {
        seconds = "0" + date.getSeconds();
    } else {
        seconds = date.getSeconds();
    }
    var now = date.getFullYear() + "-" + nowMonth + "-" + date.getDate() + " " + hours + ":" + minutes + ":" + seconds;
    $('#startDate').val(now);
    //对消入出库金额合计
    $('#totalMoney').val(0.00);
    $("#totalMoney").prop('disabled', 'disabled');
    loadSupplier();
    function loadSupplier() {
        $("#supplier").html("");  //供货单位
        $('#receiver').html('');  //收货科室
        var importClassValue = $('#importClass').val();  //入库类别 代码
        var importClassName = $('#importClass option:selected').text();//入库类别 名称
        var exportClassValue = $('#exportClass').val();   //出库类别  代码
        var exportClassName = $('#exportClass option:selected').text(); //出库类别 名称
        $.ajax({
            url: '${ctx}/csm/expSupplierCata/getSupplierOrStorageList?inoutType=' + '1' + '&type=' + importClassValue,
            type: 'get',
            contentType: 'application/json',
            async: true,
            success: function (data, textStatus, jqXHR) {
                if (data.length > 0) {
                    if (typeof(data[0].supplierJc) == 'undefined') {
                        $.each(data, function (index, item) {
                            $("#supplier").append("<option value=''></option>");
                            $("#supplier").append("<option value='" + item.storageCode + "'>" + item.storageName + "</option>");
                        });
                    } else {
                        $.each(data, function (index, item) {
                            $("#supplier").append("<option value=''></option>");
                            $("#supplier").append("<option value='" + item.id + "'>" + item.supplierJc + "</option>");
                        });
                    }
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {

            }
        });
        $.ajax({
            url: '${ctx}/csm/expSupplierCata/getSupplierOrStorageList?inoutType=' + '0' + '&type=' + exportClassValue,
            type: 'get',
            contentType: 'application/json',
            async: true,
            success: function (data, textStatus, jqXHR) {
                if (data.length > 0) {
                    if (typeof(data[0].supplierJc) == 'undefined') {
                        $.each(data, function (index, item) {
                            $("#receiver").append("<option value=''></option>");
                            $("#receiver").append("<option value='" + item.storageCode + "'>" + item.storageName + "</option>");
                        });
                    } else {
                        $.each(data, function (index, item) {
                            $("#receiver").append("<option value=''></option>");
                            $("#receiver").append("<option value='" + item.id + "'>" + item.supplierJc + "</option>");
                        });
                    }
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
            }
        });
        $('#importClass').attr('disabled',true);
        $('#exportClass').attr('disabled',true);
    }

    //弹出右边增加消耗品框
    function addExpList(){
        $("#stcokLeftDiv").attr("class","main-content col-lg-8");
        $("#stcokRightDiv").show('normal');
        return loadDiv('rigthCenterDiv','${ctx}/csm/expStock/expImpExpBalanceForm');
    }

    //删除一行消耗品
    function removeList(btn){
        $(btn).parent().parent().remove();
    }

    function saveImpExp(){
        var importClass = $('#importClass').val();  //入库类别
        var supplier = $('#supplier').val();  //供货单位
        var startDate = $('#startDate').val();  //日期
        var accountType = $('#accountType').val();  //付款方式
        var exportClass = $('#exportClass').val();  //出库类别
        var receiver = $('#receiver').val();  //收货科室
        if(importClass == null || importClass == '' || typeof(importClass) == 'undefined'){
            toastr.error('请选择入库类别!');
            return false;
        }else{
            if(importClass != '0'){
                toastr.error('对消入出库入库类别只能选择采购!');
                return false;
            }
        }
        if(supplier == null || supplier == '' || typeof(supplier) == 'undefined'){
            toastr.error('请选择供货单位!');
            return false;
        }
        if(startDate == null || startDate == '' || typeof(startDate) == 'undefined'){
            toastr.error('请选择入库日期!');
            return false;
        }
        if(accountType == null || accountType == '' || typeof(accountType) == 'undefined'){
            toastr.error('采购入库,付款方式不能为空!');
            return false;
        }
        if (exportClass == null || exportClass == '' || typeof(exportClass) == 'undefined') {
            toastr.error('请选择出库类别!');
            return false;
        } else {
            if (exportClass != '6') {
                toastr.error('对消入出库出库类别只能选择发放!');
                return false;
            }
        }
        if(receiver == null || receiver == '' || typeof(receiver) == 'undefined'){
            toastr.error('收货科室不能为空!');
            return false;
        }
        var list=$("table tr").find("td:eq(0)");
        var dataList = [];
        var data = {};
        if(list.length > 0){
            for (var i = 0; i < list.length; i++) {
                data = jQuery.parseJSON(list[i].innerText);
                console.log(data);
                console.log(startDate);
                startDate = new Date(startDate);
                data.importClass = importClass;  //入库类别
                data.supplier = supplier;  //供货单位
                data.importDate = startDate;  //入出库日期
                data.accountType = accountType;  //付款方式
                var expExportMaster = {};
                expExportMaster.exportClass = exportClass;  //出库类别
                expExportMaster.receiver = receiver;  //收货科室
                data.expExportMaster = expExportMaster;
                dataList.push(data);
            }
            var dataListJson = JSON.stringify(dataList);
            console.log(dataListJson);
            if (dataListJson == null || dataListJson == "") {
                toastr.error('没有数据保存!');
                return false;
            } else {
                $.ajax({
                    type: "POST",
                    url: '${ctx}/csm/expStock/saveImpExp',
                    data: dataListJson,
                    'contentType': 'application/json',
                    dataType: "json",
                    success: function (data) {
                        console.log(data);
                        if (data.code == 'success') {
                            toastr.success('对消入出库保存成功!');
                            return loadDiv('mainCenterDiv', '${ctx}/csm/expStock/impExpIndex');
                        } else {
                            toastr.error('网络连接错误,请检查网络');
                        }
                    },
                    error: function (data) {
                        toastr.error('网络连接错误,请检查网络');
                    }
                });
            }
        } else {
            toastr.error('没有数据保存!');
            return false;
        }
    }
    //改变进货价或数量重新计算合计金额
    function changeFun(inputId, idx) {
        if (inputId == 'quantity') {  //改变数量
            var quantity = $.trim($("#" + inputId + idx).val());
            var purchasePrice = $.trim($("#purchasePrice" + idx).val());
            $("#totalMoney").val(Number(quantity) * Number(purchasePrice));
        }
        if (inputId == 'purchasePrice') {  //改变进价
            var purchasePrice = $.trim($("#" + inputId + idx).val());
            var quantity = $.trim($("#quantity" + idx).val());
            $("#totalMoney").val(Number(quantity) * Number(purchasePrice));
        }
    }
</script>